package utils;

/**
 * @author Fang Ruichuan
 * @date 2022-11-10 9:50
 */
public class JsonUtil {

    /**
     * json数据规范化
     * @param str
     * @return
     */
    public static String jsonFormatTo(String str){
        String string= str.replace("\\\\r\\\\n","")
                .replace("\\n","")
                .replace("\\r", "")
                .replace("\\","")
                .replace("\"{","{")
                .replace("}\"","}");
        return trimBothEndsChars(string,"\"");
    }

    /**
     * 将不标准json 头和尾的 冒号 去除
     * @param srcStr
     * @param splitter
     * @return
     */
    public static String trimBothEndsChars(String srcStr, String splitter) {
        String regex = "^" + splitter + "*|" + splitter + "*$";
        return srcStr.replaceAll(regex, "");
    }


    public static void main(String[] args) {
//        String result = JsonUtil.jsonFormatTo("{\\r\\n\\\"zflx_id\":\"210\",\\r\\n\"jysj\":\"2022-11-10 08:55:32\",\\r\\n\"shh\":\"\",\\r\\n\"tranid\":\"\",\\r\\n\"zfje\":\"65\",\\r\\n\"jkje\":\"65\",\\r\\n\"tzje\":\"0\",\\r\\n\"ext\":\"\"\\r\\n}");
//        System.out.println(result);

        String format = JsonUtil.jsonFormatTo("{\\r\\n    \\\"zflx_id\\\": \\\"210\\\",\\r\\n    \\\"jysj\\\": \\\"2022-11-10 08:55:32\\\",\\r\\n    \\\"shh\\\": \\\"\\\",\\r\\n    \\\"tranid\\\": \\\"\\\",\\r\\n    \\\"zfje\\\": \\\"65\\\",\\r\\n    \\\"jkje\\\": \\\"65\\\",\\r\\n    \\\"tzje\\\": \\\"0\\\",\\r\\n    \\\"ext\\\": \\\"\\\"\\r\\n  }");
        System.out.println(format);
    }

}
